package Assets.Controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.containers.Canvas;
	import mx.core.Window;
	import mx.events.FlexNativeWindowBoundsEvent;
	import mx.controls.Alert;
	
	public class MyWindow
	{
		private var caller_object:Object = new Object;
		private var nativeWindow:Object = new Object;
		private var initWindowTitle:String;
		
		public var newWindow:Window = new Window;
		//private var newWindow:WindowedApplication;// = new WindowedApplication;
		public var windowCanvas:Canvas = new Canvas;
		
		
		
		public function MyWindow(caller:Object, container:Object, title:String)
		{
			this.caller_object=caller;
			this.nativeWindow = container;
			this.initWindowTitle = title;
			
			this.newWindow.maximizable=true;
			this.newWindow.minimizable=true;
			this.newWindow.resizable=true;
			this.newWindow.type="normal";
			//this.newWindow.width=;
			//this.newWindow.height=380;
			//this.newWindow = new WindowedApplication;
			this.setWindowTitle(title);
			
			
			this.newWindow.addEventListener(FlexNativeWindowBoundsEvent.WINDOW_RESIZE,this.windowResized);
			//this.newWindow.addEventListener(Event.CLOSE,this.windowClosed);
			//this.newWindow.addEventListener(Event.CLOSING,this.windowClosing);
			//this.openWindow();
			//this.initWindow();
			
		}
		
		private function initWindow():void
		{
			this.maximize();
			trace("inside initWindow");
			this.windowCanvas.width=this.newWindow.width - 5;
			trace("this.windowCanvas.width---->"+this.windowCanvas.width+"  this.windowCanvas.height ---->"+this.windowCanvas.height);
			if(this.newWindow.maximizable)
			{
				this.windowCanvas.height = 725;
			}
			else
			{
				this.windowCanvas.height = this.newWindow.height - 20;
			}
			this.newWindow.addChild(this.windowCanvas);
			
		}
		
		private function windowResized(event:Event):void
		{
			trace("window resizedddddddddddddddddddddddddddddddddddddddd");
			this.windowCanvas.width = this.newWindow.width - 5;
			this.windowCanvas.height = this.newWindow.height - 20;
			trace("this.windowCanvas.width---->"+this.windowCanvas.width+"  this.windowCanvas.height ---->"+this.windowCanvas.height);			
		}
		
		private function setChildrenDimension():void
		{
			var childArray:Array = new Array;
			childArray= this.windowCanvas.getChildren();
			
			for each(var displayObj:DisplayObject in childArray)
			{
				
			}
		}
		
		public function activateNativeWindow():void
		{
			this.nativeWindow.activateNativeWindow();
		}
		
		public function deactivateNativeWindow():void
		{
			this.nativeWindow.deactivateNativeContainer();
		}
		
		public function isMaximizable(state:Boolean):void
		{
			this.newWindow.maximizable=state;
		}
		
		public function maximize():void
		{
			
			this.newWindow.maximize();
			this.windowResized(new Event(FlexNativeWindowBoundsEvent.WINDOW_RESIZE));
			//this.newWindow.dispatchEvent(event:FlexNativeWindowBoundsEvent.WINDOW_RESIZE);
		}
		
		public function isMinimizable(state:Boolean):void
		{
			this.newWindow.minimizable=state;
			
		}
		
		public function isResizable(state:Boolean):void
		{
			this.newWindow.resizable=state;
		}
		
		public function closeWindow():void
		{
			var infoObj_object:Object=new Object;
			infoObj_object.message = "windowClosed";
			infoObj_object.title = this.initWindowTitle;
			this.sendInfoToView(infoObj_object);
			this.newWindow.close();
		}
		
		public function setDimensions(width:int, height:int):void
		{
			this.newWindow.width = width;
			this.newWindow.height = height;
		}
		
		public function getWidth():int
		{
			return this.windowCanvas.width;
		}
		
		public function getHeight():int
		{
			return this.windowCanvas.height;
		}
		
		/*public function addChildren(children_array:Array):void
		{
			for each(var displayObj:DisplayObject in children_array)
			{
				this.windowCanvas.addChild(displayObj);
			}
			
			this.openWindow();
			//trace("###########$$$$$$$$$"+this.caller_object+"###########$$$$$$$$$$");
		}
		*/
		public function addChildren(displayObj:DisplayObject):void
		{
			this.windowCanvas.addChild(displayObj);
			trace(" tab children added ");
		}

		public function removeChild(displayObj:DisplayObject):void
		{
			this.windowCanvas.removeChild(displayObj);
		}

		public function setWindowAppearance(infoObj_object:Object):void
		{
			
			this.windowCanvas.setStyle("backgroundImage",infoObj_object.lessonBackground);
			this.windowCanvas.setStyle("backgroundSize","100%");
			//this.windowCanvas.setStyle("backgroundColor","haloGreen");
		}
		
		public function openWindow():void
		{
			this.newWindow.open(true);
			/*if(this.newWindow.maximizable)
			{
				this.maximize();
			}*/
			this.initWindow();
		}
		
		/* private function windowClosed(event:Event):void
		{
			//this.newWindow.removeAllChildren();
			var infoObj_object:Object=new Object;
			infoObj_object.message = "windowClosed";
			infoObj_object.title = this.initWindowTitle;
			this.sendInfoToView(infoObj_object);
		}
		
		private function windowClosing(event:Event):void
		{
			if(this.newWindow.numChildren==0)
			{
				this.newWindow.removeEventListener(Event.CLOSING, this.windowClosing);
				this.newWindow.close();
				return;
			}
			
		//	Alert.show("Do you relly want to exit the lesson?","Confirmation",Alert.YES|Alert.NO,null,,null,Alert.YES);
			trace("this.newWindow.numChildren :"+this.newWindow.numChildren);
			event.preventDefault();
			
			this.newWindow.removeAllChildren();
			this.newWindow.close();
		trace("this.newWindow.numChildren :"+this.newWindow.numChildren);
			
		}
		 */
		//private function exitConfirmed
		
		private function setWindowTitle(title:String):void
		{
			this.newWindow.title = title;
		}
		
		//------------------------Communication Functions------------------------------------------//
		private function sendInfoToView(infoObj_object:Object):void
		{
			this.caller_object.incomingMessageGateway(infoObj_object);
		}
		//-----------------------------------------------------------------------------------------//
		

	}
}